package edu.clemson.wcbeck.cft;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CFTTeamSongs extends Activity{

	String[] mySongs = null;
	OnItemClickListener myListener = null;
    ArrayAdapter<String> adapter = null;
    ListView listview;
    TextView txtDisplayTeam;
    Team team;
    
	public static MediaPlayer mediaPlayer = new MediaPlayer();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teamsongs);
        
        Bundle myBundle = this.getIntent().getExtras();
        team = (Team) myBundle.getParcelable("Team");
        int myConf = team.getConfId() - 1;
        int myTeam = myBundle.getInt("team_id");
        
        txtDisplayTeam = (TextView) findViewById(R.id.txtTeamDisplay);
        listview = (ListView) findViewById(R.id.lstTeamSongs);
        
        String[] arrTeam = null; 
        String[] arrSongs = {"these", "are", "the", "default", "values", "period","now I am going",
        		"to", "add", "more", "just", "to test", "the scrolling", "on the", "ListView"};
        
        
        
        switch(myConf){
        case 0:
        	arrTeam = getResources().getStringArray(R.array.strArrACC);
        	switch(myTeam){
        	case 0:
        		arrSongs = getResources().getStringArray(R.array.BostonCollege);
        		myListener = new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int pos, long id) {
						if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
						mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.acc_bc_sons_maroon_gold);
						mediaPlayer.start();
					}
        		};
        		break;
        	case 1:
        		arrSongs = getResources().getStringArray(R.array.Clemson);
        		myListener = new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int pos, long id) {
						
						if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
						
						switch(pos){
						case 0:
							mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.acc_cu_alma);
							break;
						case 1:
							mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.acc_cu_tiger_rag);
							break;
						}
						mediaPlayer.start();
					}
        		};
        		break;
        	case 2:
        		arrSongs = getResources().getStringArray(R.array.Duke);
        		myListener = new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int pos, long id) {
						if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
						mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.acc_duke_dear_duke);
						mediaPlayer.start();
					}
        		};
        		break;
        	case 3:
        		arrSongs = getResources().getStringArray(R.array.FSU);
        		myListener = new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int pos, long id) {
						if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
						mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.acc_fsu_fight_song);
						mediaPlayer.start();
					}
        		};
        		break;
        	case 4:
        		arrSongs = getResources().getStringArray(R.array.GeorgiaTech);
        		myListener = new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int pos, long id) {
						if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
						mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.acc_gt_alma);
						mediaPlayer.start();
					}
        		};
        		break;
        	case 5:
        		arrSongs = getResources().getStringArray(R.array.Maryland);
        		break;
        	case 6:
        		arrSongs = getResources().getStringArray(R.array.Miami);
        		break;
        	case 7:
        		arrSongs = getResources().getStringArray(R.array.NCState);
        		break;
        	case 8:
        		arrSongs = getResources().getStringArray(R.array.UNC);
        		break;
        	case 9:
        		arrSongs = getResources().getStringArray(R.array.UVA);
        		break;
        	case 10:
        		arrSongs = getResources().getStringArray(R.array.VirginiaTech);
        		break;
        	case 11:
        		arrSongs = getResources().getStringArray(R.array.WakeForest);
        		break;
        	}
        	break;
        case 1:
        	arrTeam = getResources().getStringArray(R.array.strArrSEC);
        	break;
        case 2:
        	arrTeam = getResources().getStringArray(R.array.strArrBig10);
        	break;
        case 3:
        	arrTeam = getResources().getStringArray(R.array.strArrBigEast);
        	break;
        case 4:
        	arrTeam = getResources().getStringArray(R.array.strArrPac10);
        	break;
        case 5:
        	arrTeam = getResources().getStringArray(R.array.strArrBig12);
        	break;
        default:
        	arrTeam = getResources().getStringArray(R.array.strArrBig12);
        	String dummy = arrTeam[0];
        	System.out.println(dummy);
        	break;
        }
        
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, arrSongs);
        txtDisplayTeam.setText(team.getFullName());
        listview.setOnItemClickListener(myListener);
        listview.setAdapter(adapter);
        registerForContextMenu(listview);
    }
    
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId()==R.id.lstTeamSongs) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			String song = adapter.getItem(info.position);
			String[] menuItems = getResources().getStringArray(R.array.strArrContextMenuSongs);
			menu.setHeaderTitle(song + ":");

			for (int i = 0; i<menuItems.length; i++) {
				menu.add(Menu.NONE, i, i, menuItems[i]);
			}
		}
	}
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      int menuItemIndex = item.getItemId();
      long myItemID = adapter.getItemId(info.position);
//      dataSource.openDataBase();
//      
//      @SuppressWarnings("unchecked")
//      ArrayAdapter<Team> adapter = (ArrayAdapter<Team>) getListAdapter();
//	  Team team = (Team) getListAdapter().getItem(info.position);
	  
      switch(menuItemIndex){
      case 0: //Send
    	  Uri uri = Uri.parse("android.resource://edu.clemson.wcbeck.cft/raw/acc_gt_alma");// + myItemID);   
    	  Intent it = new Intent(Intent.ACTION_VIEW);   
    	  it.putExtra("sms_body", "Thanks for using CFT, enjoy!");   
    	  it.putExtra(Intent.EXTRA_STREAM, uri);  
    	  it.setType("vnd.android-dir/mms-sms");
    	  startActivity(Intent.createChooser(it, "Send MP3")); 
    	  break;
      case 1: //Save
//    	  dataSource.deleteTeam(team);
//    	  adapter.remove(team);
    	  break;
      case 2: //Set
    	  break;
      }
      
//      dataSource.close();
      return true;
    }
    
    public void findSongs(int conf, int team){
    	
    }
    
    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
    	super.onBackPressed();
    }
	
    
}
